package com.gd.data.platform.controller.api;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gd.data.platform.dto.ApiResponse;
import com.gd.data.platform.entity.DataResource;
import com.gd.data.platform.mapper.DataResourceMapper;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;

/**
 * 数据资源API控制器
 * 提供数据资源管理接口
 * 
 * @author gd-data-platform
 * @since 1.0.0
 */
@Slf4j
@RestController
@RequestMapping("/api/v1/resources")
@Api(tags = "数据资源API")
public class DataResourceController {

    @Autowired
    private DataResourceMapper dataResourceMapper;

    /**
     * 分页查询数据资源
     */
    @GetMapping("/page")
    @ApiOperation("分页查询数据资源")
    public ApiResponse<Page<DataResource>> getResources(
            @ApiParam("页码") @RequestParam(defaultValue = "1") Integer pageNum,
            @ApiParam("页大小") @RequestParam(defaultValue = "10") Integer pageSize,
            @ApiParam("资源类型") @RequestParam(required = false) String resourceType,
            @ApiParam("分类") @RequestParam(required = false) String category) {
        try {
            Page<DataResource> page = new Page<>(pageNum, pageSize);
            QueryWrapper<DataResource> queryWrapper = new QueryWrapper<>();

            if (resourceType != null) {
                queryWrapper.eq("resource_type", resourceType);
            }
            if (category != null) {
                queryWrapper.eq("category", category);
            }
            queryWrapper.eq("status", "启用");
            queryWrapper.orderByDesc("create_time");

            Page<DataResource> result = dataResourceMapper.selectPage(page, queryWrapper);
            return ApiResponse.success(result);
        } catch (Exception e) {
            log.error("查询数据资源失败", e);
            return ApiResponse.error("查询数据资源失败：" + e.getMessage());
        }
    }

    /**
     * 根据ID查询数据资源
     */
    @GetMapping("/{id}")
    @ApiOperation("根据ID查询数据资源")
    public ApiResponse<DataResource> getResourceById(@ApiParam("资源ID") @PathVariable Long id) {
        try {
            DataResource resource = dataResourceMapper.selectById(id);
            if (resource == null) {
                return ApiResponse.error("数据资源不存在");
            }
            return ApiResponse.success(resource);
        } catch (Exception e) {
            log.error("查询数据资源失败", e);
            return ApiResponse.error("查询数据资源失败：" + e.getMessage());
        }
    }

    /**
     * 根据资源ID查询数据资源
     */
    @GetMapping("/resource/{resourceId}")
    @ApiOperation("根据资源ID查询数据资源")
    public ApiResponse<DataResource> getResourceByResourceId(@ApiParam("资源ID") @PathVariable String resourceId) {
        try {
            DataResource resource = dataResourceMapper.findByResourceId(resourceId);
            if (resource == null) {
                return ApiResponse.error("数据资源不存在");
            }
            return ApiResponse.success(resource);
        } catch (Exception e) {
            log.error("查询数据资源失败", e);
            return ApiResponse.error("查询数据资源失败：" + e.getMessage());
        }
    }

    /**
     * 搜索数据资源
     */
    @GetMapping("/search")
    @ApiOperation("搜索数据资源")
    public ApiResponse<List<DataResource>> searchResources(@ApiParam("关键词") @RequestParam String keyword) {
        try {
            List<DataResource> resources = dataResourceMapper.searchResources(keyword);
            return ApiResponse.success(resources);
        } catch (Exception e) {
            log.error("搜索数据资源失败", e);
            return ApiResponse.error("搜索数据资源失败：" + e.getMessage());
        }
    }

    /**
     * 根据分类查询数据资源
     */
    @GetMapping("/category/{category}")
    @ApiOperation("根据分类查询数据资源")
    public ApiResponse<List<DataResource>> getResourcesByCategory(@ApiParam("分类") @PathVariable String category) {
        try {
            List<DataResource> resources = dataResourceMapper.findByCategory(category);
            return ApiResponse.success(resources);
        } catch (Exception e) {
            log.error("根据分类查询数据资源失败", e);
            return ApiResponse.error("根据分类查询数据资源失败：" + e.getMessage());
        }
    }

    /**
     * 统计各类型资源数量
     */
    @GetMapping("/statistics/type")
    @ApiOperation("统计各类型资源数量")
    public ApiResponse<List<Object>> getResourceTypeStatistics() {
        try {
            List<Object> statistics = dataResourceMapper.countByResourceType();
            return ApiResponse.success(statistics);
        } catch (Exception e) {
            log.error("统计资源类型数量失败", e);
            return ApiResponse.error("统计资源类型数量失败：" + e.getMessage());
        }
    }

    /**
     * 统计各分类资源数量
     */
    @GetMapping("/statistics/category")
    @ApiOperation("统计各分类资源数量")
    public ApiResponse<List<Object>> getCategoryStatistics() {
        try {
            List<Object> statistics = dataResourceMapper.countByCategory();
            return ApiResponse.success(statistics);
        } catch (Exception e) {
            log.error("统计分类数量失败", e);
            return ApiResponse.error("统计分类数量失败：" + e.getMessage());
        }
    }
}
